<?php  if ( ! defined('BASEPATH')) exit('No direct script access allowed');

class Modules{
	function Modules()
	{	
		log_message('debug', "Modules Class Initialized");
	}
	
	function run($module_path = '', $return = TRUE, $constructor_init=NULL, $method_init=NULL){
        $CI = & get_instance();
        
        $arr = Modules::search_path($module_path);
        $module_name = $arr[0];
        $module_controller = $arr[1];
        $module_method = $arr[2];
        $path = MODPATH.$arr[3].'/'.$module_name.'/controllers/'.$module_controller.EXT;

        if ( ! file_exists($path)){
			show_error('Unable to locate the module you have specified: '.$module_path.EXT);
		}

        require_once($path);
        $module_controller = ucfirst($module_controller);
        if(!is_null($constructor_init))
            $obj = new $module_controller($constructor_init);
        else
            $obj = new $module_controller;
        if(method_exists($obj, $module_method)){
            if(!is_null($method_init))
                $obj->$module_method($method_init);
            else
                $obj->$module_method();
            $view = $obj->get_view();
            $view = empty($view)?$module_method:$view;
            return $CI->parser->module_parse($module_name.'.'.$view, $obj->get_data(), $return);
        }else
            show_error('Unable to locate the module method you have specified: '.$module_method.EXT);
    }
    
    function search_path($module_path = ''){
        $CI = & get_instance();

        $site = FRONT_SITE;
        if(stripos(strtolower($module_path), strtolower(ADMIN_SITE)) === 0){
            $site = ADMIN_SITE;
            $module_path = empty($module_path)?$CI->uri->segment(2):(str_replace(strtolower(ADMIN_SITE).'.', '', strtolower($module_path)));
        }elseif(stripos(strtolower($module_path), strtolower(CLIENT_SITE)) === 0){
            $site = CLIENT_SITE;
            $module_path = empty($module_path)?$CI->uri->segment(2):(str_replace(strtolower(CLIENT_SITE).'.', '', strtolower($module_path)));
        }elseif(stripos(strtolower($module_path), strtolower(FRONT_SITE)) === 0){
            $site = FRONT_SITE;
            $module_path = empty($module_path)?$CI->uri->segment(1):(str_replace(strtolower(FRONT_SITE).'.', '', strtolower($module_path)));
        }elseif(stripos(strtolower($CI->uri->segment(1)), 'admin') === 0){
            $site = ADMIN_SITE;
            $module_path = empty($module_path)?$CI->uri->segment(2):(str_replace(strtolower(ADMIN_SITE).'.', '', strtolower($module_path)));
        }elseif(stripos(strtolower($CI->uri->segment(1)), 'client') === 0){
            $site = CLIENT_SITE;
            $module_path = empty($module_path)?$CI->uri->segment(2):(str_replace(strtolower(CLIENT_SITE).'.', '', strtolower($module_path)));
        }else{
            $site = FRONT_SITE;
            $module_path = empty($module_path)?$CI->uri->segment(1):(str_replace(strtolower(FRONT_SITE).'.', '', strtolower($module_path)));
        }
        
        $arr = explode('.', $module_path);
        if(count($arr) == 1){
            $arr[1] = $arr[0];
            $arr[2] = 'index';
        }
        
        if(count($arr) == 2){
            $arr[2] = 'index';
        }

        $arr[3] = $site;
        return $arr;
    }
}
/* End of file Loader.php */
/* Location: ./system/libraries/Loader.php */